﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace FuncWorks.Xna.UI.Menu
{
    public abstract class MenuItemWithValue : IMenuItem
    {
        public string Title { get; set; }
        public bool Enabled { get; set; }
        public bool Hidden { get; set; }
        public float Height { get; set; }
		protected abstract string DisplayedValue { get; }

        float width;
		TextAlignment align;
        Vector2 current;

		protected MenuItemWithValue(string title, TextAlignment alignment)
        {
            Title = title;
            Enabled = true;
            Hidden = false;
            align = alignment;
        }

        protected abstract void ApplyLimits();
        public abstract void Activate();
        public abstract void MoveLeft();
        public abstract void MoveRight();

        public float CalculateHeight(TextStyle textStyle, bool selected)
        {
            Vector2 size = textStyle.MeasureString(String.Format("{0}: {1}", Title, DisplayedValue));
            Height = size.Y;

			width = (align == TextAlignment.Center) ? size.X : textStyle.MeasureString(DisplayedValue).X;
            return Height;
        }

        public void Draw(SpriteBatch spriteBatch, TextStyle textStyle, float currentY, Rectangle targetRectangle, bool selected)
        {
            current.Y = currentY;
			if (align == TextAlignment.Center)
            {
                current.X = targetRectangle.Center.X - (int)Math.Ceiling(width / 2.0f);
                textStyle.DrawString(spriteBatch, String.Format("{0}: {1}", Title, DisplayedValue), current);
            }
            else
            {
                current.X = targetRectangle.Left;
                textStyle.DrawString(spriteBatch, Title, current);

                current.X = targetRectangle.Right - width;
                textStyle.DrawString(spriteBatch, DisplayedValue, current);
            }
        }

    }
}
